package cn.tedu.zhyl.base.response;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;

@ApiModel(value = "分页数据消息体",description = "分页数据统一对象")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PageResponse<T> {
    @ApiModelProperty(value = "总条目数", required = true)
    private Long total = 0L;

    @ApiModelProperty(value = "页尺寸", required = true)
    private Integer pageSize = 0;

    @ApiModelProperty(value = "总页数", required = true)
    private Long pages = 0L;

    @ApiModelProperty(value = "页码", required = true)
    private Integer page = 0;

    @ApiModelProperty(value = "数据列表", required = true)
    private List<T> list = Collections.emptyList();

    public static <T> PageResponse<T> of(PageInfo<T> page){
        return PageResponse.<T>builder()
                .page(page.getPageNum())
                .pageSize(page.getPageSize())
                .pages((long) page.getPages())
                .total(page.getTotal())
                .list(page.getList())
                .build();
    }
}
